package wec_wallet.ebo.com.activity.register;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import bassproject.ebo.com.ebobass.activity.MyBaseActivity;
import bassproject.ebo.com.ebobass.http_utils.interfaces.IHttpPostListener;
import bassproject.ebo.com.ebobass.http_utils.utils.Ipost;
import wec_wallet.ebo.com.utils.URLs;
import bassproject.ebo.com.ebobass.utils.IToast;
import bassproject.ebo.com.ebobass.utils.I_Share;
import bassproject.ebo.com.ebobass.utils.Lg;
import bassproject.ebo.com.ebobass.utils.StringUtil;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import wec_wallet.ebo.com.R;
import wec_wallet.ebo.com.utils.Functions;

public class ErrorLoginActivity extends MyBaseActivity {
    @BindView(R.id.actionbar_back)
    RelativeLayout actionbar_back;
    @BindView(R.id.btn_ok)
    Button btn_ok;
    @BindView(R.id.et_phonecode)
    EditText et_phonecode;
    @BindView(R.id.btn_get_code_phone)
    Button btn_get_code_phone;
    @BindView(R.id.tv_username)
    TextView tv_username;
    String phone, prefix;
    Handler mhandle_getcode_phone = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 1:
                    Integer v = (Integer) msg.obj;
                    if (v > 0) {
                        sendMessageDelayed(obtainMessage(1, v - 1), 1000);
                        btn_get_code_phone.setText(v.toString() + "s");
                    } else {
                        btn_get_code_phone.setClickable(true);
                        btn_get_code_phone.setText(getResources().getString(R.string.get_phone_code_send));
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_error_login);
        ButterKnife.bind(this);
        initViews();
    }

    @Override
    public void initViews() {
        phone = I_Share.readAccountUsername_Phone();
        prefix = "" + I_Share.readMobilePrefix();
        String username = "+" + prefix + " " + StringUtil.toEncryption(phone);
        tv_username.setText(username);

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onClick_Back();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {
    }

    //返回键
    @OnClick(R.id.actionbar_back)
    void onClick_Back() {
        startActivity(new Intent().setClass(ErrorLoginActivity.this,LoginActivity.class));
        finish();
    }

    //获取验证码
    @OnClick(R.id.btn_get_code_phone)
    void clickGetCode() {
//        String prefix = prefix;
//        String phone = "111111111111111";
        Map<String, Object> postParams;
        //手机号是否为空
//        if (phone == null || TextUtils.isEmpty(phone)) {
//            IToast.show(getResources().getString(R.string.login_hint_input_username));
//            return;
//        }
        //获取验证码
        postParams = new HashMap<String, Object>();
//        postParams.put("lan", I_Share.getLanguageType_String());
        postParams.put("mobile", phone);
        postParams.put("prefix", prefix);
        Functions.post(this, URLs.GET_MOBILE_CODE, postParams, new ListenerGetCode_Phone(), true, true);
    }

    //确认
    @OnClick(R.id.btn_ok)
    void onClick_OK() {
        String code = et_phonecode.getText().toString();
        Map<String, Object> postParams;
        //验证码是否为空
        if (code == null || TextUtils.isEmpty(code)) {
            IToast.show(getResources().getString(R.string.login_hint_input_phone_code));
            return;
        }
        //验证
        postParams = new HashMap<String, Object>();
//        postParams.put("lan", I_Share.getLanguageType_String());
        postParams.put("mobile", phone);
        postParams.put("prefix", prefix);
        postParams.put("code", code);
        Functions.post(this, URLs.VAIL_CODE, postParams, new ListenerVailCode(), true, true);


    }


    //接口返回 start======================================================================================================================


    //验证手机短信
    class ListenerVailCode implements IHttpPostListener {
        @Override
        public void onTimeOut() {

        }
        @Override
        public void responseSuccess(String result) {
            if (Ipost.isTimeOut(result)) {
                return;
            }
            Lg.d("验证手机短信 result=" + result);
            if (null != result) {
                try {
                    JSONObject resultObject = new JSONObject(result);
                    int error = resultObject.getInt("error");
                    String message = "";
                    if (resultObject.has("error_msg") && error != 0) {
                        message = resultObject.getString("error_msg");
                        IToast.show(message);
                    }
                    if (error == 0) {
                        I_Share.setIsLogin(true);
                        Functions.startMainActivity(ErrorLoginActivity.this);
                        finish();
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                    Lg.d(getClass() + ",接口解析异常：\n" + e.toString());
                }
            }
        }
    }



    //获取手机验证码
    class ListenerGetCode_Phone implements IHttpPostListener {
        @Override
        public void onTimeOut() {

        }
        @Override
        public void responseSuccess(String result) {
            if (Ipost.isTimeOut(result)) {
                return;
            }
            Lg.d("获取手机验证码 result=" + result);
            if (null != result) {
                try {
                    JSONObject resultObject = new JSONObject(result);
                    int error = resultObject.getInt("error");
                    String message = "";
                    if (resultObject.has("error_msg") && error != 0) {
                        message = resultObject.getString("error_msg");
                        IToast.show(message);
                    }
                    if (error == 0) {
                        IToast.show(getResources().getString(R.string.get_phone_code_success));
                        //成功
                        btn_get_code_phone.setClickable(false);
                        mhandle_getcode_phone.sendMessage(mhandle_getcode_phone.obtainMessage(1, getResources().getInteger(R.integer.resend_smscode)));
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                    Lg.d(getClass() + ",接口解析异常：\n" + e.toString());
                }
            }
        }
    }

    //接口返回 end======================================================================================================================


}
